package com.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DateSourceTest {
    //先直接使用数据源连接数据库
    @Test
    public void c3p0() throws Exception {
        //创建c3p0连接池对象
        ComboPooledDataSource c3p0=new ComboPooledDataSource();
        //通过setter方法为属性进行赋值
        c3p0.setDriverClass("com.mysql.jdbc.Driver");
        c3p0.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        c3p0.setUser("root");
        c3p0.setPassword("123456");
        //使用连接池获取连接对象
        Connection connection = c3p0.getConnection();
    }

    @Test
    public void druid() throws Exception {
        //创建druid连接池对象
        DruidDataSource druidDataSource=new DruidDataSource();
        //通过setter方法为属性进行赋值
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/test");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        //使用连接池获取连接对象
        druidDataSource.getConnection();
    }

    //测试交由spring容器管理的连接池 是否可以使用
    @Test
    public void dataSourceTest() throws SQLException {
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-context.xml");
        DataSource ds= (DataSource) applicationContext.getBean("c3p0");
        ds.getConnection();

    }


}
